package com.example.controller;

import com.example.common.Result;
import com.example.entity.Account;
import com.example.entity.Introduction;
import com.example.exception.CustomerException;
import com.example.service.IntroductionService;
import com.example.utils.TokenUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/introduction")
@CrossOrigin
public class IntroductionController {
    @Autowired
    private IntroductionService introductionService;
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Introduction> introductions = introductionService.selectAll();
        return Result.success(introductions);
    }
    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam Integer pageNum,
                             @RequestParam Integer pageSize,
                            Introduction  introduction){
        PageInfo<Introduction> pageInfo = introductionService.selectPage(pageNum, pageSize,introduction);
        return Result.success(pageInfo);
    }
    @PostMapping("/add")
    public Result add(@RequestBody Introduction introduction) {
//        Account currentUser = TokenUtils.getCurrentUser();
//        if ("USER".equals(currentUser.getRole())) {
//            throw new CustomerException("500", "您的角色暂无权限执行该操作");
//        }
        introductionService.add(introduction);
        return Result.success();
    }
    @PostMapping("/update")
    public Result update(@RequestBody Introduction introduction) {
        introductionService.update(introduction);
        return Result.success();
    }
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id) {
        introductionService.deleteById(id);
        return Result.success();
    }
//    @DeleteMapping("/deleteBatch")
//    public Result deleteBatch(@RequestBody List<Introduction> list) {
//        introductionService.deleteBatch(list);
//        return Result.success();
//    }
}
